package maitreEsclave;
import java.rmi.*;
import java.rmi.server.*;

import solveurSolo.Solveur;

import jeu.*;

public class Calcule extends UnicastRemoteObject implements CalculeInterface,Runnable{
    
	private static final long serialVersionUID = 42L;
	
    //private Listener listener;
    private Game game;
    private String maitre;
    private String port;
    private boolean a_calculer = false;
    
    public Calcule(String nomMaitre, String numeroPort) throws RemoteException {
    	this.maitre = nomMaitre;
    	this.port = numeroPort;
    	System.out.print("jesxiste, je vis");
	}

    /*public void addListener(Listener l) throws RemoteException{
    	listener =l;
    	System.out.println("ajout de"+l);
    }*/
    
   public void run(){
	   System.out.println("je me lance");
	   for(;;){
		   try {
			   Thread.sleep(100);
		   } catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			   e1.printStackTrace();
		   }
		   if(a_calculer){
			   System.out.println("je calcule");
			   Solveur.solve(game,0);
			 
			   a_calculer= false;
			   try{
				   System.out.println(maitre + "  " + port);
				   AgregationInterface somme = 
					(AgregationInterface) Naming.lookup("rmi://"+maitre+":"+port+"/Maitre");
				    somme.agrege(game);
			   }
			   catch(Exception e){
				   e.printStackTrace();
			   }
		   }
	   }
   }


    
    public void calcule(Game g) throws RemoteException {
    	System.out.println("jai recu quelque chose");
    	this.game =g;
    	this.a_calculer = true;
    }
}
